package com.app.utils.spring.interceptor;

import com.app.utils.base.http.ResultUtil;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理参数异常的拦截器
 */
@RestControllerAdvice
public class ExceptionAdvice {

	//参数类型不匹配
	//getPropertyName()获取数据类型不匹配参数名称
	//getRequiredType()实际要求客户端传递的数据类型
	@ExceptionHandler(TypeMismatchException.class)
	public String requestTypeMismatch(HttpServletRequest request, HttpServletResponse response, TypeMismatchException ex){
	    return ResultUtil.error("6001", "参数异常"+ex.getMessage());
	}

	//缺少参数异常
	//getParameterName() 缺少的参数名称
	@ExceptionHandler(ServletRequestBindingException.class)
	public String requestMissingServletRequest(HttpServletRequest request, HttpServletResponse response, MissingServletRequestParameterException ex){
	    return ResultUtil.error("6001", "参数错误"+ex.getMessage());
	}

	//空数据
//	@ExceptionHandler(NullPointerException.class)
//	public String NullPointerException(NullPointerException ex){
//		return ResultUtil.error("6001", Common.isNotEmpty(ex.getMessage())?ex.getMessage():"未获取到数据，请联系管理员");
//	}
}
